<?php
/**
 * Created by PhpStorm.
 * User: chl
 * Date: 2017/3/21 0021
 * Time: 11:22
 */
namespace app\admin\controller;
use app\models\Bases;
use think\Request;
class Integral extends Base{
    public function integralList(){
        $integralModel = new Bases('integral');
        $result['list'] = $integralModel->selectData();
        return view('integralList',['result'=>$result]);
    }
    public function integralAdd(){
        $integralModel = new Bases('integral');
        try{
            if(Request()->isPost()){
                $post = input('param.');
                $validate = $this->validate($post,'Integral.integralAdd'); //验证
                if(true !== $validate){
                    throw new \LogicException($validate,1000);
                }
                $rs = $integralModel->insertData($post);
                if($rs){
                    return self::formatSuccessResult();
                }return self::formatResult(406,'操作失败');

            }
        }catch(\Exception $e){
            return self::formatResult($e->getCode(),$e->getMessage());
        }
        return view('integralAdd');
    }
    public function integralSave(){
        $integralModel = new Bases('integral');
        try{
            $integralId = intval(input('integral_id'));
            if(Request()->isPost()){
                $post = input('param.');
                $validate = $this->validate($post,'Integral.integralSave'); //验证
                if(true !== $validate){
                    throw new \LogicException($validate,1000);
                }
                $rs = $integralModel->updateData($post);
                if($rs !== false){
                    return self::formatSuccessResult();
                }return self::formatResult(406,'操作失败');
            }
            $integralOne = $integralModel->find('integral_id ='.$integralId);
        }catch(\Exception $e){
            return self::formatResult($e->getCode(),$e->getMessage());
        }
        return view('integralSave',['result'=>$integralOne]);
    }
    public function integralDelete(){
        $integraId = intval(input('integral_id'));
        $integraModel = new Bases('integral');
        $rs = $integraModel->deleteByWhere("integral_id=".$integraId);
        if($rs !== false){
            return self::formatSuccessResult();
        }return self::formatResult(406,'操作失败');
    }
}